package com.spaceraccoons.api.adapter;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import com.spaceraccoons.api.types.DateTimeProtos;


/**
 * @author Benjamin P. Jung
 */
public class DateTimeAdapter extends ProtobufAdapter<DateTimeProtos.DateTime, DateTime> {

	@Override
	public DateTime unmarshal(final DateTimeProtos.DateTime pbDateTime) {

		final DateTime dateTime = new DateTime(
				pbDateTime.getYear(),
				pbDateTime.getMonthOfYear(),
				pbDateTime.getDayOfMonth(),
				pbDateTime.getHourOfDay(),
				pbDateTime.getMinuteOfHour(),
				pbDateTime.getSecondOfMinute(),
				pbDateTime.hasMillisOfSecond() ? pbDateTime.getMillisOfSecond() : 0,
				DateTimeZone.forID(pbDateTime.getTimeZone()));
		return dateTime;
		
	}

	@Override
	public DateTimeProtos.DateTime marshal(final DateTime dateTime) {

		return DateTimeProtos.DateTime.newBuilder()
				.setYear(dateTime.getYear())
				.setMonthOfYear(dateTime.getMonthOfYear())
				.setDayOfMonth(dateTime.getDayOfMonth())
				.setTimeZone(dateTime.getChronology().getZone().getID())
				.setHourOfDay(dateTime.getHourOfDay())
				.setMinuteOfHour(dateTime.getMinuteOfHour())
				.setSecondOfMinute(dateTime.getSecondOfMinute())
				.setMillisOfSecond(dateTime.getMillisOfSecond())
				.setTimeZone(dateTime.getChronology().getZone().getID())
				.build();

	}

}
